package com.cn.kabalifeuser.ui.order.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.order.bean.OrderExpresBean;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.utils.StringUtil;

import java.text.ParseException;
import java.time.chrono.ChronoPeriod;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 晓林 on 2020/10/10.
 * function:
 */
public class IExpressActivity extends IBaseActivity<IExpressView, IExpressPresenter> implements IExpressView {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_oreder_id)
    TextView tvOrederId;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerview;

    List<OrderExpresBean.ResultBean.DataBean> mList;
    ListAdapter mAdapter;


    Bundle bundle;
    int id;

    @Override
    protected IExpressPresenter createPresenter() {
        return new IExpressPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_express;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("物流信息");
        bundle = getIntent().getExtras();
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getMe());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new ListAdapter(R.layout.activity_express_item, mList);
        //给RecyclerView设置适配器
        recyclerview.setAdapter(mAdapter);
        id = bundle.getInt("id");
        mPresenter.onOrderExpresData(LoginHelper.getLonginData().getResult(), id);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onOrderExpresSuccess(OrderExpresBean beans) {
        if (!TextUtils.isEmpty(beans.getResult().getCom())) {
            tvName.setText(beans.getResult().getCom());
            tvOrederId.setText(beans.getResult().getNu());
            mAdapter.replaceData(beans.getResult().getData());
        } else RxToast.error(beans.getResult().getMessage());
    }

    @Override
    public void onReLoggedIn(String msg) {

        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    class ListAdapter extends BaseQuickAdapter<OrderExpresBean.ResultBean.DataBean, BaseViewHolder> {
        private List<OrderExpresBean.ResultBean.DataBean> data;

        public ListAdapter(int layoutResId, @Nullable List<OrderExpresBean.ResultBean.DataBean> data) {
            super(layoutResId, data);
            this.data = data;
        }

        @Override
        protected void convert(BaseViewHolder helper, final OrderExpresBean.ResultBean.DataBean item) {
            helper.setGone(R.id.iv_new, helper.getLayoutPosition() == 0 && data.size() > 1)
                    //当前item的索引!=0 && 物流数据的数量大于1条   ->  显示灰色小圆圈
                    .setGone(R.id.iv_old, helper.getLayoutPosition() != 0 && data.size() > 1)
                    //当前item的索引 != 物流数据的最后一条    ->  显示圆点下面长一点的灰线
                    //当前item的索引 != 物流数据的最后一条    ->  显示圆点下面长一点的灰线
                    .setGone(R.id.iv_xian, helper.getLayoutPosition() != data.size() - 1);
            helper.setText(R.id.AcceptStation, item.getContext());
            try {
                Calendar calendar = StringUtil.strToCalendar(item.getTime());
                helper.setText(R.id.AcceptTime, calendar.get(Calendar.MONTH) + 1 + "-" + calendar.get(Calendar.DAY_OF_MONTH));
                helper.setText(R.id.branch, calendar.get(Calendar.HOUR_OF_DAY) + "-" + calendar.get(Calendar.MINUTE) + "-" + calendar.get(Calendar.SECOND));

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

    }
}
